<template>
	<view class="text-success">
		<text class="font-sm">￥</text><text class="font-md font-weight-bold">{{firstAmount}}</text><text
			class="font-sm">{{secondAmount}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				firstAmount: '', //小数点前
				secondAmount: '', //小数点后
			}
		},
		props: {
			money: {
				type: String,
				default: "0"
			}
		},
		watch:{
			money:function(e){
				console.log(e);
				this.formula(e);
			}
		},
		methods: {
			formula(amount, decimalPlaces = 2) { //公式
				let amountStr = amount+'';
				// 检查小数点位置
				let decimalIndex = amountStr.indexOf('.');
				// 如果没有小数点，则在末尾添加小数点和零
				if (decimalIndex === -1) {
					amountStr += '.';
					for (let i = 0; i < decimalPlaces; i++) {
						amountStr += '0';
					}
				} else {
					let currentDecimalPlaces = amountStr.length - decimalIndex - 1;
					let difference = decimalPlaces - currentDecimalPlaces;
					for (let i = 0; i < difference; i++) {
						amountStr += '0';
					}
				}
				//将转化后的钱分成两个字段
				let splitArr = amountStr.split(".")
				this.firstAmount = splitArr[0] + '.';
				this.secondAmount = splitArr[1];
			}
		},
		mounted() {
			this.formula(this.money);
		}
	}
</script>

<style>

</style>